package com.demo.flight.controller;

import com.demo.flight.config.Result;
import com.demo.flight.config.ResultCodeEnum;
import com.demo.flight.entity.Flight;
import com.demo.flight.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ssy
 * @create 2022-03-24 9:00
 */

@CrossOrigin
@ResponseBody
@Controller
@RequestMapping("/flight")
public class FlightController {
    
    @Autowired
    private FlightService flightService;
    
    @GetMapping("/getAll")
    public Result getAllFlights() {
        List<Flight> flightList = flightService.getAll();
        return Result.success(flightList, ResultCodeEnum.SUCCESS);
    }
    
    @PostMapping("/add")
    public Result addFlight(Flight flight) {
        if (flight == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (flightService.add(flight)) {
            return Result.success();
        }
        return Result.error(ResultCodeEnum.FLIGHT_EXIST_ERROR);
    }
    
    @DeleteMapping("/delete")
    public Result deleteFlight(Integer flightId) {
        if (flightId == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (flightService.delete(flightId)) {
            return Result.success();
        }
        return Result.error(ResultCodeEnum.FLIGHT_NOT_EXIST_ERROR);
    }
    
    @PutMapping("/update")
    public Result updateFlight(Flight flight) {
        if (flight == null) {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        if (flightService.update(flight)) {
            return Result.success();
        }
        return Result.error(ResultCodeEnum.FLIGHT_NOT_EXIST_ERROR);
    
    }
}
